package com.ld.zxw.index;

import org.apache.log4j.Logger;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;

import com.ld.zxw.config.Config;
import com.ld.zxw.config.IndexFactory;
import com.ld.zxw.util.CommonUtil;

public class DeleteIndex implements IndexFactory{

	private Logger log = Logger.getLogger(DeleteIndex.class);

	@Override
	public boolean deleteAll(Config config) {
		try {

			IndexWriter indexWriter = config.getIndexWriter();
			indexWriter.deleteAll();
			CommonUtil.refresh(config, indexWriter);
			return true;
		}catch (Exception e) {
			log.error("deleteAll-error:",e);
			return false;
		}
	}

	@Override
	public boolean deletekey(Config config,Term term) {
		try {
			IndexWriter indexWriter = config.getIndexWriter();
			indexWriter.deleteDocuments(term);
			CommonUtil.refresh(config, indexWriter);
			return true;
		}catch (Exception e) {
			log.error("deletekey-error:",e);
			return false;
		}
	}
	@Override
	public boolean deletekey(Config config,Query query) {
		try {
			IndexWriter indexWriter = config.getIndexWriter();
			indexWriter.deleteDocuments(query);
			CommonUtil.refresh(config, indexWriter);
			return true;
		}catch (Exception e) {
			log.error("deletekey-error:",e);
			return false;
		}
	}
}
